package com.brsya.movie.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.brsya.base.base.BaseLayoutFragment;
import com.brsya.base.bean.PlayListBean;
import com.brsya.base.recycler.BaseAdapter;
import com.brsya.base.recycler.BaseViewHolder;
import com.brsya.base.util.UiUtil;
import com.brsya.movie.R;

import java.util.List;

import me.jessyan.autosize.AutoSize;

/**
 * Created by Brsya
 * CreateDate: 2022/5/28  15:13
 * Description:
 * 选集fragment
 */
public class PlayPartFragment extends BaseLayoutFragment {
    private LinearLayout llInfoTitle;
    private RecyclerView rlMoviePart;
    private String videoType;

    private OnSelectPartListener onSelectPartListener;

    private List<PlayListBean.PlayerListDTO.InfoListDTO> infoList;

    private BaseAdapter<PlayListBean.PlayerListDTO.InfoListDTO> partAdapter;

    private int dpMargin;

    public static PlayPartFragment newInstance(List<PlayListBean.PlayerListDTO.InfoListDTO> infoList, String videoType, OnSelectPartListener onSelectPartListener) {

        Bundle args = new Bundle();

        PlayPartFragment fragment = new PlayPartFragment();
        fragment.setArguments(args);
        fragment.infoList = infoList;
        fragment.onSelectPartListener = onSelectPartListener;
        fragment.videoType = videoType;
        return fragment;
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_play_part);
        llInfoTitle = findViewById(R.id.ll_info_title);
        rlMoviePart = findViewById(R.id.ll_movie_part);
    }

    @Override
    protected void initData() {
        AutoSize.autoConvertDensity(getActivity(), 360, true);
        llInfoTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSelectPartListener != null) {
                    onSelectPartListener.onClose();
                }
            }
        });
        dpMargin = UiUtil.dpToPx(getContext(), 5);

        partAdapter = new BaseAdapter<PlayListBean.PlayerListDTO.InfoListDTO>() {
            @Override
            public BaseViewHolder onCreate(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(getContext()).inflate(R.layout.item_part_fragment, parent, false);
                return new BaseViewHolder(view);
            }

            @Override
            public void onBind(BaseViewHolder viewHolder, int RealPosition, PlayListBean.PlayerListDTO.InfoListDTO data) {
                ConstraintLayout clPartBox = viewHolder.findViewById(R.id.cl_part_box);
                TextView tvPartText = viewHolder.findViewById(R.id.tv_part_text);
                View vPlayIcon = viewHolder.findViewById(R.id.v_play_icon);
                tvPartText.setText(data.getInfoName());
                if (data.isPlay()) {
                    vPlayIcon.setVisibility(View.VISIBLE);
                    clPartBox.setBackgroundResource(R.drawable.bg_part_select);
                    tvPartText.setTextColor(0xFF2363CA);
                } else {
                    vPlayIcon.setVisibility(View.GONE);
                    clPartBox.setBackgroundResource(R.drawable.bg_part_default);
                    tvPartText.setTextColor(0xFF6A6B6C);
                }
            }
        };

        partAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<PlayListBean.PlayerListDTO.InfoListDTO>() {
            @Override
            public void onItemClick(int position, PlayListBean.PlayerListDTO.InfoListDTO data) {
                if (onSelectPartListener != null) {
                    onSelectPartListener.onSelectPart(position, data);
                }
            }
        });
        if ("综艺".equals(videoType)) {
            rlMoviePart.setLayoutManager(new GridLayoutManager(getContext(), 1));
        }else{
            rlMoviePart.setLayoutManager(new GridLayoutManager(getContext(), 5));
        }
        rlMoviePart.setAdapter(partAdapter);

        initPartList();
    }

    private void initPartList() {
        partAdapter.setDataList(infoList);
        partAdapter.notifyDataSetChanged();
        for (int i = 0; i < infoList.size(); i++) {
            if (infoList.get(i).isPlay()) {
                rlMoviePart.scrollToPosition(i);
                break;
            }
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            AutoSize.autoConvertDensityBaseOnWidth(getActivity(), 360f);
        }
    }

    public void setInfoList(List<PlayListBean.PlayerListDTO.InfoListDTO> infoList) {
        this.infoList = infoList;
        initPartList();
    }

    public interface OnSelectPartListener {
        void onSelectPart(int index, PlayListBean.PlayerListDTO.InfoListDTO infoBean);

        void onClose();
    }
}
